package checkers3d.presentation;

import java.awt.Color;
import java.awt.Point;

/**
 * A class that represents a colored rectanglar object that may be displayed in
 * screen space as pixels.
 *
 * @author Ruben Acuna
 */
public class RenderResourceRectangle implements IRenderResource
{
    /**
     * Width in pixels of component.
     */
    int width;

    /**
     * Height in pixels of component.
     */
    int height;

    /**
     * Target color of component.
     */
    Color colorTarget;

    /**
     * Current red color of component.
     */
    float colorRed;

    /**
     * Current green color of component.
     */
    float colorGreen;

    /**
     * Current blue color of component.
     */
    float colorBlue;

    /**
     * Original color of component.
     */
    Color originalColor;

    /**
     * Class constructor that requires the size and color of the requested
     * rectangle.
     *
     * @param width Width in pixels of component.
     * @param height Height in pixels of component.
     * @param color Color of component.
     */
    public RenderResourceRectangle(int width, int height, Color color)
    {
        this(width, height, color, color);
    }

    public RenderResourceRectangle(int width, int height, Color color, Color colorTarget)
    {
        this.width = width;
        this.height = height;
        this.originalColor = color;
        this.colorTarget = colorTarget;

        colorRed = color.getRed();
        colorGreen = color.getGreen();
        colorBlue = color.getBlue();
    }
    
    /**
     * Returns the current color of the rectangle.
     *
     * @return The color of the label.
     */
    public Color getColor()
    {
        return new Color((int)colorRed, (int)colorGreen, (int)colorBlue);
    }

    /**
     * Sets the current color of the rectangle.
     *
     * @param color The color of the label.
     */
    public void setColor(Color color)
    {
        colorRed = color.getRed();
        colorGreen = color.getGreen();
        colorBlue = color.getBlue();
    }

    /**
    * Returns true if the given point is drawn by the image data, false
    * otherwise.
    *
    * @param  point A point relative to this object's image data.
    * @return       Boolean indicating if the point is drawn by the image data.
    * @see IRenderResource
    */
    public boolean containsPoint(Point point)//see interface
    {
        if(point.x > width || point.y > height ||
           point.x < 0      || point.y < 0)
            return false;
        else
            return true;
    }

    /**
     * Returns the height of this RenderResourceRectangle in pixels.
     *
     * @return Height in pixels.
     * @see IRenderResource
     */
    public int getHeight()
    {
        return height;
    }

    /**
     * Returns the width of this RenderResourceRectangle in pixels.
     *
     * @return Width in pixels.
     * @see IRenderResource
     */
    public int getWidth()
    {
        return width;
    }

    /**
     * Resets the animation of this render resource.
     */
    public void reset()
    {
        colorRed = originalColor.getRed();
        colorGreen = originalColor.getGreen();
        colorBlue = originalColor.getBlue();
    }

    /**
     * Informs this IRenderResourceBitmap that a given amount of time in
     * milliseconds has elapsed. Used for animation and time events.
     *
     * @param ms Milliseconds elapsed.
     * @see IRenderResource
     */
    public void tick(int ms)
    {
        if(!getColor().equals(colorTarget))
        {
            colorRed = morphComponent(colorRed, colorTarget.getRed(), ms);
            colorGreen = morphComponent(colorGreen, colorTarget.getRed(), ms);
            colorBlue = morphComponent(colorBlue, colorTarget.getRed(), ms);
        }
    }

    /**
     *
     * @param original
     * @param target
     * @param ms
     * @return
     */
    private float morphComponent(float original, int target, int ms) {

        float morphedColor = original;

        //morph color
        if (((int) morphedColor) != target)
            if (morphedColor > target)
            {
                morphedColor -= .1f * ms;

                if (morphedColor < 0f)
                    morphedColor = 0f;
            }
            else
            {
                morphedColor += .1f * ms;

                if (morphedColor > 255f)
                    morphedColor = 255f;
            }

        return morphedColor;
    }

    /**
    * Returns a string representation of this RenderResourceRectangle. Includes the
    * class name and a string representation its appearence.
    *
    * @return       A string representation of this RenderResourceRectangle.
    */
    @Override
    public String toString()
    {
        return getClass().getName() + " -> width: " + width +
                                         " height: " + height +
                                         " color: " + getColor();
    }

    /**
     * Informs this render resource that a given amount of time in milliseconds
     * has elapsed. Used for animation and time events.
     *
     * @param ms Milliseconds elapsed.
     */
    @Override
    public IRenderResource clone()
    {
        //RA: don't use the accessors for width/height here, they will return a scaled value.
        RenderResourceRectangle copy
                = new RenderResourceRectangle(width, height, new Color(originalColor.getRGB()), null);
                                              //new Color(getColor().getRGB()),
                                              //new Color (colorTarget.getRGB()));

        copy.colorTarget = new Color(colorTarget.getRGB());
        copy.colorRed = colorRed;
        copy.colorGreen = colorGreen;
        copy.colorBlue = colorBlue;
        originalColor = new Color(originalColor.getRGB());

        return copy;
    }
}